package com.llxwz.controller;


import com.llxwz.entity.*;
import com.llxwz.service.DiscussPostService;
import com.llxwz.service.ReportService;
import com.llxwz.service.TagService;
import com.llxwz.service.UserService;
import com.llxwz.util.CommunityConstant;
import com.llxwz.util.CommunityUtil;
import com.llxwz.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class ReportController implements CommunityConstant {

    @Autowired
    private ReportService reportService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserService userService;


    @GetMapping("/getReportList/{targetUser}/{username}/{entityType}/{status}")
    public String getReportList(@PathVariable("targetUser")String targetUser,
                                @PathVariable("username")String username,
                                @PathVariable("entityType")int entityType,
                                @PathVariable("status")int status,
                              Model model, Page page){

        page.setRows(reportService.getReportRows(targetUser,username,entityType,status));
        page.setPath("/getReportList/"+targetUser+"/"+username+"/"+entityType+"/"+status);

        List<Report> reports = reportService.getReportList(targetUser,username,entityType,status,page.getCurrent(), page.getLimit());

        List<Map<String,Object>> result = new ArrayList<>();

        if(reports!=null){
            for(Report report : reports){
                Map<String,Object> map = new HashMap<>();
                map.put("report",report);
                String entity = "";
                if(report.getEntityType()==CommunityConstant.ENTITY_TYPE_POST){
                    entity = discussPostService.findDiscussPostById(report.getEntityId()).getTitle();
                }else{
                    entity = userService.findById(report.getEntityId()).getUsername();
                }
                map.put("entity",entity);
                String tagUser = userService.findById(report.getTargetUserId()).getUsername();
                map.put("tagUser",tagUser);
                String rpUser = userService.findById(report.getUserId()).getUsername();
                map.put("rpUser",rpUser);
                result.add(map);
            }
        }

        model.addAttribute("result",result);

        return "site/admin/reportList";
    }

    @PostMapping("/addReport")
    @ResponseBody
    public String addReport(int entityType,int entityId,String content){
        User user = hostHolder.getUser();
        if (user == null){
            return CommunityUtil.getJsonString(403,"您还没有登陆!!");
        }
        Report report = new Report();
        report.setEntityType(entityType);
        report.setEntityId(entityId);
        report.setContent(content);
        report.setCreateTime(new Date());
        report.setStatus(0);

        DiscussPost post = discussPostService.findDiscussPostById(entityId);
        report.setTargetUserId(post.getUserId());
        report.setUserId(user.getId());

        reportService.addReport(report);

        return CommunityUtil.getJsonString(0,"举报成功");
    }


    @PostMapping("/dispose")
    @ResponseBody
    public String dispose(int id,int entityType,int entityId){
        if(entityType==CommunityConstant.ENTITY_TYPE_POST){
            discussPostService.updateStatus(entityId,2);
        }else{
            userService.setStatus(entityId,2);
        }
        reportService.updateStatus(id,1);
        return CommunityUtil.getJsonString(0,"处理成功");
    }

    @PostMapping("/reBack")
    @ResponseBody
    public String reBack(int id,int entityType,int entityId){
        if(entityType==CommunityConstant.ENTITY_TYPE_POST){
            discussPostService.updateStatus(entityId,0);
        }else{
            userService.setStatus(entityId,1);
        }
        reportService.updateStatus(id,2);
        return CommunityUtil.getJsonString(0,"驳回成功");
    }

}
